package com.dlc.shop.config;


import cn.hutool.core.util.StrUtil;
import com.dlc.shop.common.bean.*;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.constants.PaypalConstant;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.foreign.ipPortMap;
import com.dlc.shop.common.jky.JkyMp;
import com.dlc.shop.common.poly.PolyMp;
import com.dlc.shop.common.poly.PolyRouteorderMp;
import com.dlc.shop.service.SysConfigService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 商城配置文件
 * 支付配置、文件上传配置、短信配置、快递配置、小程序配置、微信网页开发配置、公众号配置、菠萝派配置
 *
 * @author yami
 */
@Component
@AllArgsConstructor
public class ShopConfig {

    private final SysConfigService sysConfigService;

    public AliLive getAliLive() {
        return sysConfigService.getSysConfigObject(Constant.ALI_LIVE_CONFIG, AliLive.class);
    }

    public Sms4jConfig getSmsConfig() {
        Sms4jConfig sms4jConfig = sysConfigService.getSysConfigObject(Constant.SMS_CONFIG, Sms4jConfig.class);
        if (sms4jConfig == null || StrUtil.isBlank(sms4jConfig.getAccessKeyId())) {
            // 无法获取短信配置，无法发送短信
            throw new YamiShopBindException("yami.sys.sms.no.exist");
        }
        return sms4jConfig;
    }


    public QuickBird getQuickBird() {
        return sysConfigService.getSysConfigObject(Constant.QUICKBIRD_CONFIG, QuickBird.class);
    }

    public Quick100 getQuick100() {
        return sysConfigService.getSysConfigObject(Constant.QUICK100_CONFIG, Quick100.class);
    }

    public QuickAli getAliQuick() {
        return sysConfigService.getSysConfigObject(Constant.ALI_QUICK_CONFIG, QuickAli.class);
    }

    public QuickCdek getCdekQuick() {
        return sysConfigService.getSysConfigObject(Constant.QUICK_CDEK_CONFIG, QuickCdek.class);
    }

    public WxMiniApp getWxMiniApp() {
        WxMiniApp wxMiniApp = sysConfigService.getSysConfigObject(Constant.MA_CONFIG, WxMiniApp.class);
        if (wxMiniApp == null || StrUtil.isBlank(wxMiniApp.getAppId())) {
            // 请在后台页面设置微信小程序信息后，再进行操作
            throw new YamiShopBindException("yami.sys.wxMa.no.exist");
        }
        return wxMiniApp;
    }

    public WxMiniApp getWxMiniAppToTradeManaged() {
        return sysConfigService.getSysConfigObject(Constant.MA_CONFIG, WxMiniApp.class);
    }

    public WxMp getWxMp() {
        WxMp wxMp = sysConfigService.getSysConfigObject(Constant.MP_CONFIG, WxMp.class);
        if (wxMp == null || StrUtil.isBlank(wxMp.getAppId())) {
            // 请在后台页面设置微信公众号信息后，再进行操作
            throw new YamiShopBindException("yami.sys.wxMp.no.exist");
        }
        return wxMp;
    }


    public WxApp getWxApp() {
        WxApp wxApp = sysConfigService.getSysConfigObject(Constant.MX_APP_CONFIG, WxApp.class);
        if (wxApp == null || StrUtil.isBlank(wxApp.getAppId())) {
            // 请在后台页面设置微信开放平申请应用相关信息后，再进行操作
            throw new YamiShopBindException("yami.sys.wxApp.no.exist");
        }
        return wxApp;
    }

    public WxPay getWxPay() {
        WxPay wxPay = sysConfigService.getSysConfigObject(Constant.WXPAY_CONFIG, WxPay.class);
        if (wxPay == null || StrUtil.isBlank(wxPay.getMchId())) {
            // 请在后台页面设置微信支付信息后，再进行操作
            throw new YamiShopBindException("yami.sys.wxPay.no.exist");
        }
        return wxPay;
    }

    public Alipay getAlipay() {

        return sysConfigService.getSysConfigObject(Constant.ALIPAY_CONFIG, Alipay.class);
    }

    public Domain getDomain() {
        Domain domain = sysConfigService.getSysConfigObject(Constant.DOMAIN_CONFIG, Domain.class);
        if (domain == null || StrUtil.isBlank(domain.getApiDomainName())) {
            // 请在后台页面设置回调域名后，再进行操作
            throw new YamiShopBindException("yami.sys.wxDomain.no.exist");
        }
        return domain;
    }

    public PayPal getPayPal() {
        PayPal payPal = sysConfigService.getSysConfigObject(Constant.PAYPAL_CONFIG, PayPal.class);
        boolean is = Objects.isNull(payPal)
                || StrUtil.isBlank(payPal.getClientId())
                || StrUtil.isBlank(payPal.getClientSecret());
        if (is) {
            // 请在后台页面设置paypal相关信息后，再进行操作
            throw new YamiShopBindException("yami.sys.paypal.no.exist");
        }
        if (StrUtil.isBlank(payPal.getMode())) {
            payPal.setMode(PaypalConstant.PAYPAL_MODE);
        }
        return payPal;
    }

    public ExchangeRate getExchangeRate() {
        ExchangeRate exchangeRate = sysConfigService.getSysConfigObject(Constant.EXCHANGE_RATE_CONFIG, ExchangeRate.class);
        if (exchangeRate == null) {
            // 请在后台页面设置paypal相关信息后，再进行操作
            throw new YamiShopBindException("yami.sys.paypal.no.exist");
        }
        return exchangeRate;
    }


    public LangConfig getLang() {
        return sysConfigService.getSysConfigObject(Constant.INTERNATIONALIZATION_CONFIG, LangConfig.class);
    }

    /**
     * 获取文件上传配置
     *
     * @return 文件上传配置
     */
    public UploadFile getUploadFile() {
        return sysConfigService.getSysConfigObject(Constant.UPLOAD_FILE_CONFIG, UploadFile.class);
    }

    private String getEbusinessIdByStr(String paramValue) {
        String[] split = paramValue.split(",");
        for (String s : split) {
            if (s.contains("eBusinessID")) {
                String[] split1 = s.split(":");
                String str = split1[1];
                return str.replace("\"", "");
            }
        }
        return "";
    }

    public PolyMp getPoly() {
        PolyMp polyMp = sysConfigService.getSysConfigObject(Constant.POLY_CONFIG, PolyMp.class);
        if (polyMp == null || StrUtil.isBlank(polyMp.getAppKey())) {
            // 请在后台页面设置微信公众号信息后，再进行操作
            throw new YamiShopBindException("yami.sys.polyMp.no.exist");
        }
        return polyMp;
    }

    public JkyMp getJky() {
        JkyMp jkyMp = sysConfigService.getSysConfigObject(Constant.JKY_CONFIG, JkyMp.class);
        if (jkyMp == null || StrUtil.isBlank(jkyMp.getAppKey())) {
            // 请在后台页面设置微信公众号信息后，再进行操作
            throw new YamiShopBindException("yami.sys.polyMp.no.exist");
        }
        return jkyMp;
    }

    public PolyRouteorderMp getPolyRouteorder() {
        PolyRouteorderMp polyMp = sysConfigService.getSysConfigObject(Constant.POLY_ROUTEORDER_CONFIG, PolyRouteorderMp.class);
        if (polyMp == null || StrUtil.isBlank(polyMp.getUrl())) {
            // 请在后台页面设置微信公众号信息后，再进行操作
            throw new YamiShopBindException("yami.sys.PolyRouteorderMp.no.exist");
        }
        return polyMp;
    }

    public ipPortMap getForeignUrl() {

        ipPortMap ipPortMap = sysConfigService.getSysConfigObject(Constant.URL_CONFIG, ipPortMap.class);
        if (ipPortMap == null ) {
            // 请在后台页面设置微信公众号信息后，再进行操作
            throw new YamiShopBindException("yami.sys.urlConfigRu.no.exist");
        }
        return ipPortMap;
    }
}
